
#ifndef CHANWATCHER_H
#define CHANWATCHER_H

#include <QObject>
#include <QTimer>
#include <QThread>
#include "components/threadid.h"
#include "boardwatchparameters.h"

class MainWindow;
namespace Wishmaster
{
	class Chan;
	class Board;
	class ChanManager;

	class ChanWatcher : public QObject
	{
		Q_OBJECT
	public:
		ChanWatcher();
		virtual ~ChanWatcher();

		void saveSettings();
		void loadSettings();

		void setManager(ChanManager* manager);
		void setView(MainWindow* view);

		bool startTimers();

		enum RefreshPolicy
		{
			rpAllChans = 1,
			rpCurrentChan = 2,
			rpCurrentBoard = 3,
			rpNothing
		};

		void setBoardWatchParameters(const BoardWatchParameters& params);
		void removeBoardWatchParameters(const QString& chanName, const QString& boardId);
		bool isBoardInWatchList(const QString& chanName, const QString& boardId) const;
		BoardWatchParameters boardWatchParameters(const QString& chanName, const QString& boardId) const;

		void addToWatchList(const ThreadId& threadId);
		void removeFromWatchList(const ThreadId& threadId);
		QList<ThreadId> watchList() const;


	signals:
		void statusMessage(const QString& msg, int timeout = 0);

	protected:
		virtual QTimer* createTimer(QObject* parent = 0);
	
	private slots:
		void autoRefresh();
		void watchRefresh();
		void boardWatchTimeout();

	private:
		void refreshAllChans(bool ignoreOverrides);
		void refreshChan(Chan* chan, bool ignoreOverrides);
		void refreshBoard(Board* board);

		void destroyBoardWatch(const QString& chanName, const QString& boardId);

		ChanManager* m_manager;
		MainWindow* m_view;

		QTimer m_watchTimer;
		QTimer m_swapTimer;
		QTimer m_autoRefreshTimer;
		QThread* m_swapThread;
		QList<ThreadId> m_watchList;

		struct BoardWatchEntry
		{
			BoardWatchParameters params;
			QTimer* timer;
		};

		QList<BoardWatchEntry> m_boardWatches;
	};
}

#endif

